﻿// ReSharper disable SuggestUseVarKeywordEvident
using System;
using System.Windows.Forms;
using bnab.DataModel;

namespace bnab
{
  public partial class SettingsForm : Form
  {
    public SettingsForm()
    {
      InitializeComponent();
      CustomInitializeComponent();
    }

    private void CustomInitializeComponent()
    {
      tbLogin.Text = Settings.Login;
      tbPassword.Text = Settings.Password;

      switch(Settings.ProxyChoice)
      {
        case ProxyChoice.DoNotUse:
          rbNoProxy.Checked = true;
          break;

        case ProxyChoice.UseDefault:
          rbDefaultProxy.Checked = true;
          break;

        case ProxyChoice.UseCustom:
          rbCustomProxy.Checked = true;
          break;

        default:
          throw new ArgumentOutOfRangeException();
      }
      tbProxyUrl.Text = Settings.ProxyUrl;
      nudProxyPort.Value = Settings.ProxyPort;
      tbProxyLogin.Text = Settings.ProxyLogin;
      tbProxyPassword.Text = Settings.ProxyPassword;
      tbBalanceLimit.Text = Settings.BalanceWarning.ToString();

      EnableCustomProxyControls(false);
      tabControl1.SelectedTab = tpAccount;

      EnableCustomProxyControls(rbCustomProxy.Checked);
    }

    private void chbMask_CheckedChanged(object sender, EventArgs e)
    {
      tbPassword.UseSystemPasswordChar = chbMask.Checked;
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
      if (!ValidateChildren())
      {
        return;
      }
      Settings.Login = tbLogin.Text;
      Settings.Password = tbPassword.Text;
      Settings.ProxyUrl = tbProxyUrl.Text;
      Settings.ProxyPort = (ushort) nudProxyPort.Value;
      Settings.ProxyLogin = tbProxyLogin.Text;
      Settings.ProxyPassword = tbProxyPassword.Text;
      Settings.BalanceWarning = int.Parse(tbBalanceLimit.Text.Trim());

      Settings.ProxyChoice = rbNoProxy.Checked
                               ? ProxyChoice.DoNotUse
                               : rbDefaultProxy.Checked
                                    ? ProxyChoice.UseDefault
                                    : ProxyChoice.UseCustom;

      Settings.Save();
      // Close();
    }

    private void rbCustomProxy_Click(object sender, EventArgs e)
    {
      EnableCustomProxyControls(rbCustomProxy.Checked);
    }

    private void EnableCustomProxyControls(bool enable)
    {
      tbProxyLogin.Enabled = enable;
      nudProxyPort.Enabled = enable;
      tbProxyUrl.Enabled = enable;
      tbProxyPassword.Enabled = enable;
    }

    private void tbBalanceLimit_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
      int dummy;
      if (int.TryParse(tbBalanceLimit.Text.Trim(), out dummy))
      {
        errorProvider1.SetError(tbBalanceLimit, string.Empty);
        e.Cancel = false;
      }
      else
      {
        errorProvider1.SetError(tbBalanceLimit, "Должно быть целое число");
        e.Cancel = true;
      }
    }

    private void tbLogin_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
      if (string.IsNullOrEmpty(tbLogin.Text.Trim()))
      {
        errorProvider1.SetError(tbLogin, "Нужно заполнить логин");
        e.Cancel = true;
      }
      else
      {
        errorProvider1.SetError(tbLogin, string.Empty);
      }
    }

    private void tbPassword_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
      if (string.IsNullOrEmpty(tbPassword.Text.Trim()))
      {
        errorProvider1.SetError(tbPassword, "Нужно заполнить пароль");
        e.Cancel = true;
      }
      else
      {
        errorProvider1.SetError(tbPassword, string.Empty);
      }
    }

    private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
    {
    }
  }
}
// ReSharper restore SuggestUseVarKeywordEvident

